package com.mapbased.cc;

import com.mapbased.wfw.GlobalRef;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mapbased.wfw.Config;
import org.springframework.context.ApplicationContext;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class SpringAppContext
{

    protected ApplicationContext context;
    private GlobalRef ref;
    public SpringAppContext(GlobalRef ref)
    {

        this.ref=ref;
        try
        {
            this.reload();
        }

        catch(Exception e)
        {
            e.printStackTrace();
            throw new java.lang.InternalError(e.toString());
        }

    }

//    public static SpringAppContext getInstance()
//    {
//        if(instance == null)
//        {
//            throw new java.lang.InstantiationError(
//                "Must load SpringAppContext by a servlet container");
//        }
//        return instance;
//    }

    public ApplicationContext getApplicationContext()
    {
        return this.context;
    }

    public Object getBean(String s)
    {
        return this.context.getBean(s);
    }

    public void reload()
    {
        this.ref.getServletContext().log(
         this.ref.getServletContext().getServletContextName()+
        " attendtion: AppContext loading!!");

        String s=ref.getWfwConfig().getValue("beanconfig");
        if(s.equals(""))

        {
            s=ref.getWfwConfig().getValue("bean-config");
        }
        if(s.equals(""))
        {
            ref.getServletContext().log("Waring:Cannot get attribute[beanconfig] from wfw-config file, appContext will be unusable!");
            this.context=null;
            // return;
        }
        else
        {
            String[] ss=s.split(";");
            this.context=new ClassPathXmlApplicationContext(ss);

        }
        // staticContextRef=this.context;


    }

}
